package de.duester.genealogy.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.duester.genealogy.PersonEditActivity;
import de.duester.genealogy.PersonEventEditActivity;
import de.duester.genealogy.R;
import de.duester.genealogy.Utility;
import de.duester.genealogy.model.Date;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.Person;
import de.duester.genealogy.model.PersonEvent;
import de.duester.genealogy.model.RoleCode;
import de.duester.genealogy.model.dao.PersonEventDao.Properties;
import de.duester.genealogy.model.service.EventService;

public class EventListAdapter extends ArrayAdapter<Event> implements Comparator<Event> {
	private Activity activity;
	private Person person;
	private List<Event> events;

	public EventListAdapter(Context context, int textViewResourceId, Person person,
			List<Event> events) {
		super(context, textViewResourceId, events);
		activity = (Activity) context;
		this.person = person;
		this.events = events;
		Collections.sort(this.events, this);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v;

		if (convertView == null) {
			LayoutInflater li = activity.getLayoutInflater();
			v = li.inflate(R.layout.event_list_info, null);
		} else
			v = convertView;

		TextView tvType = (TextView) v.findViewById(R.id.tvType);
		TextView tvRole = (TextView) v.findViewById(R.id.tvRole);
		TextView tvDate = (TextView) v.findViewById(R.id.tvDate);

		String tc = events.get(position).getTypeCode();
		String name = EventService.getType(tc);

		String otherName = null;
		if (tc.equals("birth")) {
			Person child = EventService.getPerson(events.get(position), "child");
			if (!person.equals(child))
				otherName = child.getName();
		} else if (tc.equals("marriage")) {
			Person husband = EventService.getPerson(events.get(position), "husband");
			Person wife = EventService.getPerson(events.get(position), "wife");

			if (person.equals(husband))
				otherName = wife.toString();
			else if (person.equals(wife))
				otherName = husband.toString();
			else
				otherName = husband.toString() + " + " + wife.toString();
		}
		if (otherName != null)
			name += " (" + otherName + ")";

		tvType.setText(name);

		List<PersonEvent> pe = Utility
				.getDaoSession()
				.getPersonEventDao()
				.queryBuilder()
				.where(Properties.PersonId.eq(person.getId()),
						Properties.EventId.eq(events.get(position).getId())).list();
		if (pe.size() > 0) {
			String roleCode = pe.get(0).getRoleCode();
			RoleCode rc = EventService.getRole(roleCode);
			tvRole.setText(activity.getString(R.string.txtRole, rc.getRole()));
		}

		Date date = EventService.getDate(events.get(position), "date");
		if (date != null)
			tvDate.setText(date.toString());

		v.setBackgroundResource(R.drawable.list_element);

		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((PersonEditActivity) activity).savePerson();
				Utility.getDB().beginTransaction();
				Intent intent = new Intent(activity, PersonEventEditActivity.class);
				intent.putExtra("personId", person.getId().longValue());
				intent.putExtra("eventId", events.get(position).getId().longValue());
				intent.putExtra("eventTypeCode", events.get(position).getTypeCode());
				activity.startActivityForResult(intent, 0);
			}
		});

		return v;
	}

	@Override
	public int compare(Event lhs, Event rhs) {
		return lhs.compare(rhs);
	}
}
